from bs4 import BeautifulSoup

with open('C:\\Users\\mmciel\\Desktop\\ddd.txt', 'r', encoding="utf-8") as f:
    data = f.read()


bf = BeautifulSoup(data, features="lxml")
content = bf.findAll("div",attrs={"id":"content"})
if len(content) != 1:
    pass
content = content[0]
# 找片名和年份
name_year_temp = content.findAll("h1")[0].findAll("span")
mv_en_name = name_year_temp[0].string
mv_release = (int)(name_year_temp[1].string[1:-1])

score_temp = content.findAll("strong",attrs={"class":"ll rating_num"})
mv_score = (float)(score_temp[0].string)
# print(mv_score)

all_info_temp = content.findAll("div",attrs={"id":"info"})[0]
all_info_temp = all_info_temp.text.split("\n")
mv_director = []
mv_star = []
mv_writer = []
mv_type = []
mv_region = ""
mv_length = 0
mv_introduce = ""
for item in all_info_temp:
    if len(item) < 2:
        continue
    kv_list = item.replace(" ","").split(":")
    if len(kv_list) < 2:
        continue
    if kv_list[0] == "导演":
        data = kv_list[1].split("/")
        if len(data) > 2:
            data = data[0:2]
        mv_director.extend(data)
    if kv_list[0] == "编剧":
        data = kv_list[1].split("/")
        if len(data) > 2:
            data = data[0:2]
        mv_writer.extend(data)
    if kv_list[0] == "主演":
        data = kv_list[1].split("/")
        if len(data) > 4:
            data = data[0:4]
        mv_star.extend(data)
    if kv_list[0] == "类型":
        data = kv_list[1].split("/")
        mv_type.extend(data)
    if kv_list[0] == "制片国家/地区":
        data = kv_list[1].split("/")[0]
        mv_region=data
    if kv_list[0] == "片长":
        data = kv_list[1].split("/")[0]
        data = data.split("分钟")[0]
        mv_length = data

print(mv_director)
print(mv_star)
print(mv_writer)
print(mv_type)
print(mv_region)
print(mv_length)
print(mv_introduce)


def test2():# top250测试
    with open('C:\\Users\\mmciel\\Desktop\\ddd.txt', 'r', encoding="utf-8") as f:
        data = f.read()

    # print(data)

    bf = BeautifulSoup(data, features="lxml")
    page_table = bf.findAll("ol")

    # print(len(page_table))
    if len(page_table) != 1:
        pass
    page_div_pic = bf.findAll("div", attrs={"class": "pic"})
    print(len(page_div_pic))

    for pic in page_div_pic:
        mv_top = pic.findAll("em")[0].string
        mv_url = pic.findAll("a")[0]["href"]
        mv_id = mv_url.split("/")[-2]
        mv_name = pic.findAll("img")[0]["alt"]
        mv_img_url = pic.findAll("img")[0]["src"]
        print()
        break

